package ${package}<#if moduleName != "">.${moduleName}<#else></#if>.utils;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
* 返回数据
*/
@Data
public class R  {
    // 数据
    private HashMap<String, Object> data = new HashMap<>();

    // 新增数据
    public R put(String key, Object value) {
        data.put(key,value);
        return this;
    }

    // 去除数据
    public R remove(String key) {
        data.remove(key);
        return this;
    }

    public R() {
        data.put("code", 0);
    }

    public R(Integer code, Object message) {
        data.put("code",code);
        data.put("msg",message);
    }

    public static R error() {
        R r = new R();
        return r;
    }

    public static R error(String msg) {
        R r = new R(500, msg);
        return r;
    }

    public static R error(int code, String msg) {
        R r = new R(code, msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("code",200);
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.put("code",200);
        map.forEach(r::put);
        return r;
    }

    public static R ok() {
        R r = new R();
        r.put("code",200);
        return r;
    }
}

